package com.jishuo.nontax.api.socket.handle;

import com.jishuo.base.bean.BaseResult;
import com.jishuo.base.bean.ErrorCodeEnum;
import com.jishuo.base.util.SpringContextUtil;
import com.jishuo.nontax.api.socket.ApiHandle;
import com.jishuo.nontax.bean.vo.WaitaffirmPayInfoVO;
import com.jishuo.nontax.bfc.service.WaitaffirmPayInfoService;
import org.dom4j.Element;

import java.math.BigDecimal;

/**
 * @author hlp
 * @date 2023/2/8 15:16
 * @Description: 支出 业务层
 */
public class WaitaffirmPayInfoApi implements ApiHandle {
    @Override
    public BaseResult execute(String head, Element body) {
        if (head.equals("setWaitaffirmPayInfo")) { //  新增修改 支出记录
            //获取参数值并赋值对象
            WaitaffirmPayInfoVO infoVO = new WaitaffirmPayInfoVO();
            infoVO.setAccountcode(body.elementText("accountcode"));
            infoVO.setPayeeaccountnum(body.elementText("payeeaccountnum"));
            infoVO.setPayeeaccountname(body.elementText("payeeaccountname"));
            infoVO.setPostscript(body.elementText("postscript"));
            infoVO.setBankaffirm(body.elementText("bankaffirm"));
            infoVO.setPaybankdt(body.elementText("paybankdt"));
            infoVO.setOriginfund(new BigDecimal(body.elementText("originfund")));

            String cantonid = body.elementText("cantonid");
            infoVO.setCantonid(cantonid.length() == 0 ? null : Integer.valueOf(cantonid));
            String unitid = body.elementText("unitid");
            infoVO.setUnitid(unitid.length() == 0 ? null : Long.valueOf(unitid));
            String itemid = body.elementText("itemid");
            infoVO.setItemid(itemid.length() == 0 ? null : Long.valueOf(itemid));

            if(infoVO.getBankaffirm()==null||infoVO.getBankaffirm().equals("")){
                return new BaseResult(ErrorCodeEnum.PARAM_ERR.getCode(), "参数为空[bankaffirm]");
            }

            WaitaffirmPayInfoService waitaffirmPayInfoService = (WaitaffirmPayInfoService) SpringContextUtil.getBean("waitaffirmPayInfoService");
            BaseResult ret = waitaffirmPayInfoService.setWaitaffirmPayInfo(infoVO);
            int n = (ret.isSuccess()) ? 1 : 0;
            ret.setData("<returninfo>"+n+"</returninfo>");
            return ret;

        } else if (head.equals("sendWaitAffirmPayInfo")) { // 查询支出信息 发送到财政
            //获取参数值
            String accountcode = body.elementText("accountcode");
            String paybankdt = body.elementText("paybankdt");

            if(accountcode==null||accountcode.equals("")){
                return new BaseResult(ErrorCodeEnum.PARAM_ERR.getCode(), "参数为空[accountcode]");
            }

            WaitaffirmPayInfoService waitaffirmPayInfoService = (WaitaffirmPayInfoService) SpringContextUtil.getBean("waitaffirmPayInfoService");

            BaseResult ret = waitaffirmPayInfoService.sendWaitAffirmPayInfo(null, accountcode, paybankdt);
            int n = (ret.isSuccess()) ? 1 : 0;
            ret.setData("<returninfo>"+n+"</returninfo>");
            return ret;
        }
        return null;
    }
}
